/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JavaClass;

import Main.ExpoManagerMain;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mai Lan
 */
public class ActionAll {
    public static  void LoadInfor(Vector vCP,JTable tb,Vector feil,String NameTb){
         try {
            String sqlLoad = "select * from "+NameTb;
            Statement stLoad = ExpoManagerMain.conn.createStatement();
            ResultSet rs = stLoad.executeQuery(sqlLoad);
           
            Vector row;
            Vector vDB=new Vector();
            while (rs.next()) {
                 row=new Vector();
                 for(int i=0;i<feil.size();i++){
                    row.add(rs.getString(feil.get(i).toString()));
                 }
                
             
                 vDB.add(row);
            }

            tb.setModel(new DefaultTableModel(vDB, vCP));
        } catch (SQLException ex) {
            Logger.getLogger(ActionAll.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
     public static  void LoadInfor(Vector vCP,JTable tb,Vector feil,String NameTb1,String Nametb2,String coltb1,String col2){
         try {
            String sqlLoad = "select * from "+NameTb1+","+Nametb2+" where "+NameTb1+"."+coltb1+" = "+Nametb2+"."+col2;
            Statement stLoad = ExpoManagerMain.conn.createStatement();
            ResultSet rs = stLoad.executeQuery(sqlLoad);

            Vector row;
            Vector vDB=new Vector();
            while (rs.next()) {
                 row=new Vector();
                 for(int i=0;i<feil.size();i++){
                    row.add(rs.getString(feil.get(i).toString()));
                 }


                 vDB.add(row);
            }

            tb.setModel(new DefaultTableModel(vDB, vCP));
        } catch (SQLException ex) {
            Logger.getLogger(ActionAll.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
//    public static void insertInfor(String tbName,Vector feil,Vector feilformadd){
//        try {
//            String sqlInsert = "Insert into "+tbName;
//            sqlInsert+="(";
//            for(int i=0;i<feil.size();i++){
//                sqlInsert+=feil.get(i).toString();
//                sqlInsert+=",";
//            }
//                 sqlInsert+=") values (";
//            for(int i=0;i<feil.size();i++){
//                if( i==0){
//                    sqlInsert+="?";
//                }else{
//                     sqlInsert+=",?";
//                }
//
//            }
//                sqlInsert+=")";
//             System.out.println(sqlInsert);
//            PreparedStatement psInsert = ExpoManagerMain.conn.prepareStatement(sqlInsert);
//
//            for(int i=1;i<feilformadd.size();i++){
//
//
//
//                  psInsert.setString(i, feilformadd.get(i).toString());
//
////                psInsert.setString(2, new String(passPW.getPassword()));
////                psInsert.setString(3, txtEmail.getText());
////                psInsert.setString(4, txtNameAd.getText());
////                psInsert.setString(5, (String) cobSuperAd.getSelectedItem());
//
//                System.out.println(i);
//
//            }
//            psInsert.executeUpdate();
//        } catch (SQLException ex) {
//            Logger.getLogger(ActionAll.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//    }
    public static  void deleteInfor(JTable tb,String tbName,String FielID){
           int n=tb.getSelectedRow();
           if(tb.isCellSelected(tb.getSelectedRow(), tb.getSelectedColumn())==false){
                JOptionPane.showMessageDialog(null, "Please choice admin want to update!!");

           }else {
            try {

                int Id;
                Id = Integer.parseInt((String) tb.getValueAt(n, 0));
                  Statement stUp = ExpoManagerMain.conn.createStatement();
                  String sqldeAd="delete from "+tbName+" where "+FielID+"="+Id;
                stUp.executeUpdate(sqldeAd);

                
            } catch (SQLException ex) {
                Logger.getLogger(ActionAll.class.getName()).log(Level.SEVERE, null, ex);
            }
           

           }

    }
    
}
